package ithink.link;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.Properties;

/**
 * Created by zaqb on 17-10-24.
 */
public class ConfigHelper {
    private static String name;
    private static String version;
    private static Integer userPort;
    private static String key;
    private static Integer connectionTTL;
    private static String influxIp;
    private static Integer influxPort;
    private static String memcachedHost = "m-m5e0cfce11af6a44.memcache.rds.aliyuncs.com";//控制台上的“内网地址”
    private static String memcachedPort ="11211"; //默认端口 11211，不用改
    private static String memcachedUsername = "OCS_DEV";//控制台上的“实例ID”，新版ocs的username可以置空
    private static String memcachedPassword = "g593tAys05ts8ttn";//邮件中
    private static Integer memcachedExpire = 30;
    private static String mqAccessKey;
    private static String mqAccessSecret;
    private static String mqAddr;
    private static String mqConsumerId;
    private static String mqProducerId;
    private static String mqTopic;
    private static Logger LOGGER = LoggerFactory.getLogger(ConfigHelper.class);
    public static boolean load() throws Exception {
        Properties prop = new Properties();
        String config = System.getProperty("user.dir") + "/server.properties";
        InputStream in = new FileInputStream(new File(config));
        prop.load(in);
        if(!prop.containsKey("name")) {
            LOGGER.error( "config no key name" );
            return false;
        }
        if(!prop.containsKey("version")) {
            LOGGER.error( "config no key version" );
            return false;
        }
        if(!prop.containsKey("key")) {
            LOGGER.error( "config no key key" );
            return false;
        }
        if(!prop.containsKey("user_port")) {
            LOGGER.error( "config no key user_port" );
            return false;
        }


        if(!prop.containsKey("connection_ttl")) {
            LOGGER.error( "config no key connection_ttl" );
            return false;
        }
        if(!prop.containsKey("influx_ip")) {
            LOGGER.error( "config no key influx_ip" );
            return false;
        }
        if(!prop.containsKey("influx_port")) {
            LOGGER.error( "config no key influx_port" );
            return false;
        }

        if(!prop.containsKey("memcached_host")) {
            LOGGER.error( "config no key memcached_host" );
            return false;
        }
        if(!prop.containsKey("memcached_port")) {
            LOGGER.error( "config no key memcached_port" );
            return false;
        }
        if(!prop.containsKey("memcached_username")) {
            LOGGER.error( "config no key memcached_username" );
            return false;
        }
        if(!prop.containsKey("memcached_password")) {
            LOGGER.error( "config no key memcached_password" );
            return false;
        }
        if(!prop.containsKey("memcached_expire")) {
            LOGGER.error( "config no key memcached_expire" );
            return false;
        }

        if(!prop.containsKey("mq_access_key")) {
            LOGGER.error( "config no key mq_access_key" );
            return false;
        }

        if(!prop.containsKey("mq_access_secret")) {
            LOGGER.error( "config no key mq_access_secret" );
            return false;
        }
        if(!prop.containsKey("mq_addr")) {
            LOGGER.error( "config no key mq_addr" );
            return false;
        }
        if(!prop.containsKey("mq_consumer_id")) {
            LOGGER.error( "config no key mq_consumer_id" );
            return false;
        }
        if(!prop.containsKey("mq_producer_id")) {
            LOGGER.error( "config no key mq_producer_id" );
            return false;
        }
        if(!prop.containsKey("mq_topic")) {
            LOGGER.error( "config no key mq_topic" );
            return false;
        }

        name = prop.getProperty("name").trim();
        version = prop.getProperty("version").trim();
        key = prop.getProperty("key").trim();
        userPort = Integer.parseInt(prop.getProperty("user_port").trim());

        connectionTTL = Integer.parseInt(prop.getProperty("connection_ttl").trim());
        influxIp = prop.getProperty("influx_ip").trim();
        influxPort = Integer.parseInt(prop.getProperty("influx_port").trim());
        memcachedHost = prop.getProperty("memcached_host").trim();
        memcachedPort = prop.getProperty("memcached_port").trim();
        memcachedUsername = prop.getProperty("memcached_username").trim();
        memcachedPassword = prop.getProperty("memcached_password").trim();
        memcachedExpire = Integer.parseInt(prop.getProperty("memcached_expire").trim());

        mqAccessKey = prop.getProperty("mq_access_key").trim();
        mqAccessSecret = prop.getProperty("mq_access_secret").trim();
        mqAddr = prop.getProperty("mq_addr").trim();
        mqConsumerId = prop.getProperty("mq_consumer_id").trim();
        mqProducerId = prop.getProperty("mq_producer_id").trim();
        mqTopic = prop.getProperty("mq_topic").trim();

        return true;
    }


    public static String getName() {
        return name;
    }


    public static String getVersion() {
        return version;
    }

    public static Integer getUserPort() {
        return userPort;
    }


    public static String getKey() {
        return key;
    }

    public static Integer getConnectionTTL() {
        return connectionTTL;
    }

    public static String getInfluxIp() {
        return influxIp;
    }

    public static Integer getInfluxPort() {
        return influxPort;
    }

    public static String getMemcachedHost() {
        return memcachedHost;
    }

    public static String getMemcachedPort() {
        return memcachedPort;
    }

    public static String getMemcachedUsername() {
        return memcachedUsername;
    }

    public static String getMemcachedPassword() {
        return memcachedPassword;
    }

    public static Integer getMemcachedExpire() {
        return memcachedExpire;
    }

    public static String getMqAccessKey() {
        return mqAccessKey;
    }

    public static String getMqAccessSecret() {
        return mqAccessSecret;
    }

    public static String getMqAddr() {
        return mqAddr;
    }

    public static String getMqConsumerId() {
        return mqConsumerId;
    }

    public static String getMqProducerId() {
        return mqProducerId;
    }

    public static String getMqTopic() {
        return mqTopic;
    }

    public static void main(String[] args) {
        ByteBuf body = PooledByteBufAllocator.DEFAULT.heapBuffer(1024);
        System.out.println(body.capacity());
        System.out.println(body.array().length);
    }
}
